package com.it.config;

import com.it.interceptor.WebServiceAuthInterceptor;
import com.it.service.MessageService;
import org.apache.cxf.Bus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.xml.ws.Endpoint;

@Configuration
public class CXFConfig {

    private final Bus bus;
    private final MessageService messageService;
    private final WebServiceAuthInterceptor webServiceAuthInterceptor;

    @Autowired
    public CXFConfig(Bus bus, MessageService messageService, WebServiceAuthInterceptor webServiceAuthInterceptor) {
        this.bus = bus;
        this.messageService = messageService;
        this.webServiceAuthInterceptor = webServiceAuthInterceptor;
    }

    public ServletRegistrationBean getServletRegistrationBean() {
        return new ServletRegistrationBean(new CXFServlet(), "/services/*"); // 设置webservice访问父路径
    }

    @Bean
    public Endpoint getMessageEndpoint() {
        EndpointImpl endpoint = new EndpointImpl(bus, messageService);
        endpoint.publish("/MessageService");
        endpoint.getInInterceptors().add(webServiceAuthInterceptor); // 添加拦截器
        return endpoint;
    }
}
